package com.umpay.business.consume.impl;

import com.umpay.business.consume.ConsumeH5PayBusiness;
import com.umpay.dict.DataDict;
import com.umpay.dict.retcode.MerAccessEC;
import com.umpay.dto.UMFResponse.Message;
import com.umpay.dto.UMFResponse.Message.Link;
import com.umpay.dto.UMFResponse.Message.Meta;
import com.umpay.dto.merAccess.req.consume.ConsumeH5Req;
import com.umpay.dto.merAccess.res.consume.ConsumeH5Res;
import com.umpay.dto.merBusi.req.consume.ConsumeH5BusiReq;
import com.umpay.dto.merBusi.res.consume.ConsumeH5BusiRes;
import com.umpay.exception.BusinessException;
import com.umpay.feign.client.ConsumeBusiServerFeignClient;
import com.umpay.util.PropertyUtil;
import com.umpay.util.RpidUtils;
import com.umpay.util.StringUtils;
import com.umpay.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
/**
 * 
 * @ClassName: ConsumeH5PayBusinessImpl  
 * @Description: TODO(这里用一句话描述这个类的作用)  
 * @author zhangtao  
 * @date Mar 13, 2018
 */
@Component
public class ConsumeH5PayBusinessImpl implements ConsumeH5PayBusiness {
    
	private static final Logger log = LoggerFactory.getLogger(ConsumeH5PayBusinessImpl.class);
	
	@Autowired
	private ConsumeBusiServerFeignClient consumeBusiServerFeignClient;
	
	@Autowired 
	private PropertyUtil propertyUtil;
	
	@Override
	public Message<ConsumeH5Res> consumeH5Pay(ConsumeH5Req consumeH5Req) {
		log.info("【H5消费--步骤1】请求参数校验");
		checkRequestParameter(consumeH5Req);
		log.info("【H5消费--步骤2】请求业务线支付下单");
		Message<ConsumeH5BusiRes> merBusiRes = requestMerBusiPayOrder(consumeH5Req);
		log.info("【H5消费--步骤3】响应结果通知商户");
		return assembleResponseData(merBusiRes);
	}
	private void checkRequestParameter(ConsumeH5Req consumeH5Req) {
		String payType = StringUtils.trim(consumeH5Req.getPay_type());
		if(!(payType.equals(DataDict.WECHAT) || payType.equals(DataDict.ALIPAY))) {
			throw new BusinessException(MerAccessEC.REQPARAM_ERR, "H5消费请求参数[pay_type]格式不正确");
		}
	}
	private Message<ConsumeH5BusiRes> requestMerBusiPayOrder(ConsumeH5Req consumeH5Req){
		ConsumeH5BusiReq merBusiReq = new ConsumeH5BusiReq();
		String payType = StringUtils.trim(consumeH5Req.getPay_type());
		merBusiReq.setRpid(RpidUtils.getRpid());
		merBusiReq.setMerId(StringUtils.trim(consumeH5Req.getMer_id()));
		merBusiReq.setbProductId(StringUtils.trim(propertyUtil.getProperty("bsp.bproductid"))); //基础业务产品
		if(payType.equals(DataDict.WECHAT)) {
			merBusiReq.setpProductId(StringUtils.trim(propertyUtil.getProperty("bsp.wechat.pay.pproductid"))); //支付产品号
		}
		if(payType.equals(DataDict.ALIPAY)) {
			merBusiReq.setpProductId(StringUtils.trim(propertyUtil.getProperty("bsp.alipay.pay.pproductid"))); //支付产品号
		}
		merBusiReq.setReqDate(TimeUtil.date8());
		merBusiReq.setReqTime(TimeUtil.time6());
		merBusiReq.setTradeNo(StringUtils.trim(consumeH5Req.getTrade_no()));
		merBusiReq.setPayType(StringUtils.trim(consumeH5Req.getPay_type()));
		merBusiReq.setAmount(StringUtils.trim(consumeH5Req.getAmount()));
		merBusiReq.setUserIp(StringUtils.trim(consumeH5Req.getUser_ip()));
		merBusiReq.setVersion(StringUtils.trim(consumeH5Req.getVersion()));
		merBusiReq.setMerTrace(StringUtils.trim(consumeH5Req.getMer_trace()));
		merBusiReq.setRetUrl(StringUtils.trim(consumeH5Req.getRet_url()));
		merBusiReq.setNotifyUrl(StringUtils.trim(consumeH5Req.getNotify_url()));
		merBusiReq.setRemark(StringUtils.trim(consumeH5Req.getRemark()));
		merBusiReq.setAccessType(StringUtils.trim(consumeH5Req.getAccessType()));
		merBusiReq.setAccessName(StringUtils.trim(consumeH5Req.getAccessName()));
		merBusiReq.setDepBankMerId(StringUtils.trim(consumeH5Req.getDepBankMerId()));
		log.info(String.format("H5支付下单：调用远程服务接口请求参数为:%s", merBusiReq));
		Message<ConsumeH5BusiRes> merBusiRes = consumeBusiServerFeignClient.consumeH5Pay(merBusiReq);
		log.info(String.format("H5支付下单：调用远程服务接口请求参数为:%s", merBusiReq));
		return merBusiRes;
	}
	private Message<ConsumeH5Res> assembleResponseData(Message<ConsumeH5BusiRes> merBusiRes){
		Message<ConsumeH5Res> resMessage = new Message<ConsumeH5Res>();
		Meta resMeta = new Meta();
		List<Link> links = new ArrayList<Link>();
		if(merBusiRes != null) {
			Meta merBusiRet = merBusiRes.getMeta();
			String retCode = merBusiRet.getRet_code();
			String retMsg = merBusiRet.getRet_msg();
			resMeta.setRet_code(retCode);//返回码
			resMeta.setRet_msg(retMsg);//返回描述
			ConsumeH5BusiRes resInfo = merBusiRes.getData();
			ConsumeH5Res merAccessRes = new ConsumeH5Res();
			if(resInfo != null) {
				merAccessRes.setMer_id(resInfo.getMerId());//商户号
				merAccessRes.setTrade_no(resInfo.getTradeNo());
				merAccessRes.setVersion(resInfo.getVersion());
				merAccessRes.setAmount(resInfo.getAmount());
				merAccessRes.setCom_amt(resInfo.getComAmt());
				merAccessRes.setMer_date(resInfo.getOrderDate());
				merAccessRes.setMer_trace(resInfo.getMerTrace());
				merAccessRes.setOrder_id(resInfo.getOrderId());
				Link link = new Link(resInfo.getPaymentLink(),String.valueOf(HttpMethod.GET),null);
				links.add(link);
			}
			resMessage.setData(merAccessRes);
		}else {
			resMeta.setRet_code(MerAccessEC.RESULT_UNKNOWN);
			resMeta.setRet_msg("交易结果不明");
		}
		resMessage.setMeta(resMeta);
		resMessage.setLinks(links);
		return resMessage;
	}
}
